<?php
namespace App\Services;

use App\Librarys\Alipay\AopCertClient;
use App\Librarys\Alipay\request\AlipayFundTransUniTransferRequest;

class AlipayService
{
    // 去除支付宝账号中的错误
    public static function cleanAlipayNumber($alipay)
    {
        $keywords = ["\u00A0","\u0020","\u3000","<","+","\\","/","、","0x","insert","select","update","delete","drop","truncate","declare","exec","execute","#",";","*","-","%","'","\"","(",")","?","~","!","$","^","&"];
        // 去空格的支付宝
        $alipays = json_encode(preg_replace('/\s+/', '', $alipay));
        $lastalipay = str_replace($keywords,"",$alipays);
        return $lastalipay;
    }

    // 支付宝转账
    public static function alipayTransfer($order_id,$trans_amount,$identity,$name)
    {
    	// 这个用的和转账不不是一个应用，这个用的基础服务；避免上线影响支付
        // 文档地址 https://opendocs.alipay.com/support/01rfze
        $aop = new AopCertClient();
        $appCertPath = public_path()."/alipay_cert_982/appCertPublicKey_2019092867906164.crt";
        $alipayCertPath = public_path()."/alipay_cert_982/alipayCertPublicKey_RSA2.crt";
        $rootCertPath = public_path()."/alipay_cert_982/alipayRootCert.crt";

        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = "2019092867906164";
        $aop->rsaPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCfnGNcRd9L+0tZ1mdhMmvOOuDMTgTg3PupFobRhvPiNWL7SBD1WxhOgnW5mo1Xgn2dv+yadtZzyecL5yNx8eEChu6xRIiEwREWoj3W2RwsFKadnhd2qQysi5x+JjAICCaxBpIkSyvXJuLfhgiPTogys5FG9ECSNyk+C9XOciDIPIbOmAdmTBCIYsEPHdA95KR6DkHcFYZ8LcYnQ8KvsjSrCA8WiePj0svrolTiwmKICV54cH+nmnf5pJ8IalZ2gciVMqd6phqq6yl0p12qaDUsnCHeQM0Q4TAJwZJJULPSIVKAcsoYyNYmon2FPDL6e7zau5c4o65e/G5CLmldEtv1AgMBAAECggEAZTJyjP6AJVye2cbmpOmjEI6kGOUekrOJnBt8PzyhR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6v2CPzRqvlSVmfQKrcTvEluUAybDAUKfqNaSQ98zefqc7JNIX4iLNphCiSXLf05rd5yTrJ4gws1fQPL2qUQaUjqdIiWjzLAAIDdgw';
        $aop->format = "json";
        $aop->charset= "GBK";
        $aop->signType= "RSA2";
        //调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
        //是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->isCheckAlipayPublicCert = true;
        //调用getCertSN获取证书序列号
        $aop->appCertSN = $aop->getCertSN($appCertPath);
        //调用getRootCertSN获取支付宝根证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);
        $request = new AlipayFundTransUniTransferRequest();
        $bizcontent = [
            "out_biz_no" => $order_id,
            "trans_amount" => $trans_amount,
            "product_code" => "TRANS_ACCOUNT_NO_PWD",
            "biz_scene" => "DIRECT_TRANSFER",
            "order_title" => "账户余额提现",
            "payee_info" => [
                "identity" => $identity,
                "identity_type" => "ALIPAY_LOGON_ID",
                "name" => $name,
            ],
            "remark" => ""
        ];
        $request->setBizContent(json_encode($bizcontent));
        $result = $aop->execute($request); 
        // $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        // $resultCode = $result->$responseNode->code;
        return $result->alipay_fund_trans_uni_transfer_response;
    }

}